package com.vivachek.protocol.process

import com.vivachek.protocol.converts.*
import com.vivachek.protocol.exception.NotSupportProtocolException

internal class RealDataProcess : AbsProcess() {

    private val commands = mutableListOf(
        SNConvert(),
        UnitConvert(),
        SetTimeConvert(),
        DeviceSoftwareVersionConvert(),
        DevicePowerOffConvert(),
        StatusConvert(),
        CodeChipConvert(),
        DeviceTimeConvert(),
        DeleteHistoryConvert(),
        HistoryConvert(),
        HistoryEndConvert()
    )

    override fun convert(data: List<Int>,origin:List<Int>) {
        val commandCode = data.first()
        val expandCode = data[1]
        val len = data[2] + data[3]
        val realData = data.subList(4, len + 4)
        commands.forEach {
            if (it.hit(commandCode, expandCode,len)) {
                it.listener = listener
                it.parse(realData,origin)
                return
            }
        }
        throw NotSupportProtocolException("无法匹配对应命令码和扩展码")
    }
}